 <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>人工发送短信</title>
    <link href="../../layui/css/iconfont/iconfont.css" rel="stylesheet" type="text/css">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.10.2.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
    <script src="../../js/util.js"></script>
    <script>
    
     function back(){
    location.href = "smsArtificialRecordManage.html";
    }
        layui.use(['form', 'jquery', 'element', 'laydate', 'layer', 'upload'], function () {
            var form = layui.form;
            var $ = layui.$;
            var laydate = layui.laydate;
            var layer = layui.layer;
            var upload = layui.upload;
            var batchNumber;//批次号
            laydate.render({
                elem: '#sendTime'
                , type: 'datetime'
                ,value:new Date()
            });
            upload.render({
                elem: '#import'
                , url: '/artificialRecord/upload'
                , auto: false //关闭自动上传
                , accept: 'file' //普通文件
                , exts: 'csv' //只允许上传csv文件
                //,multiple: true
                , bindAction: '#upload'
                , before: function (obj) {
                    var mgrId=$('#mgrId').val().trim();
                    var mgrName_id=$('#mgrName_id').val();
                    var mgrName=$('#mgrName').val().trim();
                    
                    this.data = {
                        cityId: $('#cityId').val(),
                        countyId: $('#countyId').val(),
                        mgrId: $('#mgrId').val(),
                        mgrNameId:$('#mgrName_id').val(),
                        remark: $('#remark').val(),
                        sendTime: $('#sendTime').val(),
                        batchNumber: batchNumber
                    };                                       
                    layer.load(); //上传loading                                      
                }
                , done: function (obj) {
                    layer.closeAll('loading'); //关闭loading                                                                                                                                                         
                    if (obj.code == 0) {
                        var data = obj.data;
                        var num = data.toString().split(",");
                        layer.open({
                            title: '文件上传'
                            , content: '文件上传成功，在人工发送记录查询界面可查看导入和发送结果'
                        });
                    } else {
                        layer.msg(obj.msg)
                    }
                }
                , error: function (index, upload) {
                    layer.closeAll('loading'); //关闭loading
                    layer.msg("上传过程中出现异常，请重试");
                }

            });
            
            
            $('#mgrName').bind('input propertychange', function(){
            //获取input 元素,并实时监听用户输入
                $("#mgrName_id").val("");           
            });
            var jsonData=[];
            $("#mgrName").keyup(function(){
                
                var key=$("#mgrName").val();
                var mgrId=$("#mgrId").val();
                var url = '/util/selectMgrNameLikeContent';
                var param = {
                    mgrId: mgrId,
                    key:key
                };
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        data:param,
                        success: function (json) {
                            jsonData = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
			}); 
			//联想
            $("#mgrName").autocomplete({
               source: function( request, response ) {
                    var a = [];
                    jsonData.forEach(function(item){
                         a.push({
                                 // 设置item信息
                                 label: item.name, // 下拉项显示内容
                                 value: item.name,   // 下拉项对应数值
                                 id: item.id,      //自定义字段
                            });
                     });
                     response(a);
               },
    			minLength : 2, // 输入框字符个等于2时开始查询
    			select : function(event, ui) { // 选中某项时执行的操作
    				//存放选中选项的信息
    				$("#mgrName_id").val(ui.item.id);
    				$("#mgrName").val(ui.item.name);
    
    				var url = '/content/findContentByMgrId';
    				var param = {
    					mgrId : ui.item.id
    				};
    				$.ajax({
    					type : 'post',
    					async : false,
    					url : url,
    					data : param,
    					beforeSend : function() {
    						loading = layer.load(0, {
    							shade : false
    						});
    					},
    					success : function(json) {
    						layer.close(loading);
    						if (json.code == 0) {
    							var data = json.data;
    							$("#content").val(data.content);
    							form.render();
    						} else {
    							layer.msg(json.msg);
    						}
    					},
    					error : function(data) {
    						layer.msg('系统异常，请联系管理员处理!');
    					}
    				});
    			}
    		});
    
    
    		//身份识别+初始化编码
    		$(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/artificialRecord/initBatchNumber',
                    success: function (json) {
                        if (json.code == 0) {
                            batchNumber = json.data;
                            $("#batchNumber").html("批次号:"+batchNumber);
                        } else {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });

                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        user = json;
                        if (json.duty == 1) {//地市
                        $("#cityId").val(json.cityId);
                        $("#cityId").attr("disabled", true);
                        var data3 = selectMgrInfo(json.cityId);
                        appendMgr(data3);
                         var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                        form.render();
                    } else if (json.duty == 2) {//区县
                        $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data3 = selectMgrInfo(json.countyId);
                            appendMgr(data3);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                       // appendMgr(json.list);                       
                    }else if (json.duty == 3) {//项目
                            appendMgr(json.list);
                            var data3 = selectMgrInfo(json.countyId);
                            appendMgr(data3);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                        }
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            });

           //监听区县
            form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data1 = selectMgrInfo(data.value);
                 appendMgr(data1);
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            
            //监听项目
            form.on('select(countyId)', function (data) {
            if(data.value == ""){appendMgr(select(cityId))}
            else{
                var data3 = selectMgrInfo(data.value);
                appendMgr(data3);}
            });
                        //根据城市ID查询项目
            function selectMgrInfo(cityId) {
                var data;
                var url = '/util/findByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
                      
            //根据城市ID和区县查询项目
            function selectMgrInfo(countyId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + countyId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该区县下没有项目</option>');
                }
                form.render();
            }
                              
            form.on('select(mgrId)', function (mgrId)  {
               var mgrId = $("#mgrId").val();
               if(mgrId==""){
               layer.msg('请选择一个项目'); 
               }else{
                var url = '/content/findContentByMgrId';
                var param = {
                    mgrId: mgrId
                };
                $.ajax({
                    type: 'post',
                    async: false,
                    url: url,
                    data: param,
                    beforeSend: function () {
                        loading = layer.load(0, {shade: false});
                    },
                    success: function (json) {
                        layer.close(loading);
                        if(json.code==0){
                            var data = json.data;
                            $("#content").val(data.content);
                            form.render();
                        }else {
                            layer.msg(json.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
                }
            });
            
        });
    </script>
</head>
<body>

<!--container-->
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
              <span class="layui-breadcrumb" lay-separator="|">
<a href="javascript:returnIndex2()">首页&nbsp;</a>
  <a href=""> &nbsp;短信发送管理&nbsp; </a>
  <a href=""> &nbsp;人工发送短信&nbsp; </a>
</span>
        </div>
        <div class="content-box">
            <h4 class="tips-h4"><i class="iconfont icon-xinxi"></i>
                <div id="batchNumber" class="layui-inline"></div>
            </h4>
				<div class="layui-form layui-form-pane">
					<div class="layui-form-item">
						归属地：
						<div class="layui-inline">
							<label class="layui-form-label">地市</label>
							<div class="layui-input-block" style="width: 150px;">
								<select name="cityId" lay-filter="cityId" id="cityId">
									<option value="">请选择</option>
									<option value="11">苏州</option>
									<option value="12">淮安</option>
									<option value="13">宿迁</option>
									<option value="14">南京</option>
									<option value="15">连云港</option>
									<option value="16">徐州</option>
									<option value="17">常州</option>
									<option value="18">镇江</option>
									<option value="19">无锡</option>
									<option value="20">南通</option>
									<option value="21">泰州</option>
									<option value="22">盐城</option>
									<option value="23">扬州</option>
									<option value="25">省级</option>
								</select>
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">区县</label>
							<div class="layui-input-block" style="width:150px">
								<select name="countyId" lay-filter="countyId" id="countyId">
									<option value="">全部</option>
								</select>
							</div>
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">项目简称</label>
							<div class="layui-input-block" style="width:500px">
								<input type="text" name="mgrName" id="mgrName"
									lay-verify="required" value=""
                                autocomplete="on"
									class="layui-input" />
									<input type="hidden" id="mgrName_id" />
							</div>
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">项目名称</label>
							<div class="layui-input-block">
								<select name="mgrId" lay-filter="mgrId" id="mgrId">
									<option value="">请先选择一个项目</option>
								</select>
							</div>
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">发送内容</label>
						<div class="layui-input-block">
							<textarea class="layui-textarea" disabled id="content"
								style="width:30%"></textarea>
						</div>
					</div>


					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">发送时间</label>
							<div class="layui-input-inline">
								<input type="text" class="layui-input" id="sendTime"
									placeholder="请选择">
							</div>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">发送理由</label>
						<div class="layui-input-block">
							<textarea placeholder="请输入内容" class="layui-textarea"
								style="width:30%" id="remark"></textarea>
						</div>
					</div>

                    <div class="layui-form-item" style="display: flex;">
					<label class="layui-form-label">发送号码</label>
					<div class="layui-input-block" style= "margin-left: 0px;">
						<input type="text" name="number" id="number"
							lay-verify="required" placeholder="" autocomplete="on"
							class="layui-input" style="width:100%" />
					</div>										 
						<button type="button" class="layui-btn" id="import">
							<i class="layui-icon"></i>发送号码录入
						</button>
						<span><a href="/artificialRecord/download">（人工发送模板下载）</a></span>
					</div>
			
				<div class="layui-form-item">
                    <button class="layui-btn layui-btn-normal" id="upload" >上传</button>
                     <button class="layui-btn layui-btn-normal" type="button"
									onclick="back()">返回</button>
                                </button>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</body>
</html>